<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@page import="com.googlecode.contests_repo.core.users.AccessRight"%>
<%@page import="com.googlecode.contests_repo.core.users.UserManager"%>
<%@page import="java.util.regex.Pattern"%>
<%@page import="java.util.Comparator"%>
<%@page import="java.util.Arrays"%>
<%@page import="java.net.URLDecoder"%>
<%@page import="java.net.URLEncoder"%>
<%@page import="com.googlecode.contests_repo.core.config.ConfigurationManager"%>
<%@page import="java.io.File"%>
<%@page import="com.googlecode.contests_repo.core.model.ResourcesUtils"%>

<%
	String loggedUser = (String) session.getAttribute("username");

	String resourcePath = request.getParameter("path");
	String repoRoot = ConfigurationManager.getInstance().getRepoRoot();
	String absolutePath = repoRoot + File.separator + resourcePath;
%>

	<h3>
		<%=resourcePath.replaceAll(Pattern.quote(File.separator),
					" > ")%>:
	</h3>
	<%
		boolean canEdit = "1".equals(request.getParameter("edit"));
		String editParameter = canEdit ? "&amp;edit=1" : "";

		File folder = new File(absolutePath);
		if (!folder.exists()
				&& folder.getName().matches("_public_files|_files")) {
			folder.mkdir();
		}
		File[] files = folder.listFiles();
		if (files != null) {

			Arrays.sort(files, new Comparator<File>() {
				public int compare(File f1, File f2) {
					if (f1.isDirectory() && !f2.isDirectory()) {
						return -1;
					}

					if (f2.isDirectory() && !f1.isDirectory()) {
						return 1;
					}

					return f1.getName().compareTo(f2.getName());
				}
			});
	%>

	<h4>Content:</h4>
	<table>
		<%
			for (File file : files) {
					String path = resourcePath + File.separator
							+ file.getName();
					String viewLink = null;
					if (file.isDirectory()) {
						viewLink = "resource.jsp?path=" + path;
					} else {
						viewLink = "download_problem_resources.jsp?path=" + path;
					}
					String deleteLink = "delete_file?path=" + path;
		%>
		<tr>
			<td><a href="<%=viewLink + editParameter%>"> <%=file.getName()%>
			</a></td>
			<%
				if (canEdit
								&& UserManager.hasAccessRight(loggedUser,
										absolutePath, AccessRight.VIEW_FULL)) {
			%>
			<td><a href="<%=deleteLink%>"> delete </a></td>
			<%
				}
			%>
		</tr>
		<%
			}
		%>
	</table>
	<%
		}
	%>

	<%
		if (canEdit
				&& UserManager.hasAccessRight(loggedUser, absolutePath,
						AccessRight.VIEW_FULL)) {
	%>
	<form method="post" action="new_folder">
		<fieldset>
			<legend>New folder</legend>
			<label>Name: </label> <input type="text"
				name="folderName" /> <input type="hidden" name="location"
				value="<%=resourcePath%>" /> <input type="submit" value="Create" />
		</fieldset>
	</form>
	<form method="post" action="upload_file" enctype="multipart/form-data">
		<fieldset>
			<legend>Upload file</legend>
			<input type="hidden" name="location" value="<%=resourcePath%>" /> <input
				type="file" name="file" value="Browse" /> <input type="submit"
				value="Upload" />
		</fieldset>
	</form>
	<%
		}
	%>
